package com.social.servlet;

import com.social.dao.CommentDAO;
import com.social.model.Comment;
import com.social.model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/CommentLog")
public class CommentLogServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        int userId = ((User) session.getAttribute("user")).getId();
        int logId = Integer.parseInt(request.getParameter("logId"));
        String content = request.getParameter("content");
        boolean isLike = Boolean.parseBoolean(request.getParameter("isLike"));

        Comment comment = new Comment();
        comment.setLogId(logId);
        comment.setUserId(userId);
        comment.setContent(content);
        comment.setIsLike(isLike);

        CommentDAO commentDAO = new CommentDAO();
        commentDAO.addComment(comment);
        request.getRequestDispatcher("commentLog.jsp").forward(request, response);
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 这里编写处理GET请求的逻辑
        // 例如，可以让它转发到对应的JSP页面进行展示相关表单等操作
        request.getRequestDispatcher("commentLog.jsp").forward(request, response);
    }
}